package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.Rank;
import com.blueheart.music.service.RankService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class RankController {
    @Autowired
    private RankService rankService;

    /**
     * 查询评分通过歌单id
     * @param songListId
     * @return
     */
    @PostMapping("/rank/findRank")
    public ResultUtil findRank(@RequestParam("songListId") Integer songListId){
        if (songListId>0){
            List<Rank> bySongListId = rankService.findBySongListId(songListId);
            return ResultUtil.success(bySongListId);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 增加评分
     * @param request
     * @return
     */
    @PostMapping("/rank/addScore")
    public ResultUtil addScore(HttpServletRequest request){
        String songListId = request.getParameter(MyConst.SONGLISTID);
        String consumerId = request.getParameter(MyConst.CONSUMERID);
        String score = request.getParameter(MyConst.SCORE);
        if (StringUtils.isEmpty(songListId)||StringUtils.isEmpty(consumerId)||StringUtils.isEmpty(score)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Rank rank = new Rank();
        rank.setSongListId(new Integer(songListId));
        rank.setConsumerId(new Integer(consumerId));
        rank.setScore(new Integer(score));
        boolean flag = rankService.addScore(rank);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.ADD_ERROR);
    }

}
